package com.itchen.controller.module;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itchen.common.PageResult;
import com.itchen.common.Result;
import com.itchen.domain.entity.OperationLog;
import com.itchen.domain.entity.User;
import com.itchen.service.OperationLogService;
import com.itchen.utils.JwtTokenUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import com.itchen.annotation.Log;

import java.util.List;

/**
 * 操作日志 Controller
 */
@Tag(name = "操作日志管理", description = "操作日志相关接口")
@RestController
@RequestMapping("/operationLog")
public class OperationLogController {

    private final OperationLogService operationLogService;

    public OperationLogController(OperationLogService operationLogService) {
        this.operationLogService = operationLogService;
    }

    @GetMapping("getAll")
    @Operation(summary = "获取所有操作日志信息")
    public Result<List<OperationLog>> getAllOperationLogs() {
        List<OperationLog> logs = operationLogService.list();
        return Result.success(logs);
    }

    @GetMapping("getByPage")
    @Log(operationType = "查询", operationModule = "日志管理")
    public Result<PageResult<OperationLog>> getAllOperationLogsByPage(
            Long pageNum,
            Long pageSize,
            String username,
            String operationType,
            String operationModule) {

        // 获取登录用户信息
        User currentUser = JwtTokenUtils.getCurrentUser();

        // 创建查询条件
        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();

        // 动态添加查询条件

        if (currentUser != null) {
            if (currentUser.getRole().equals("ADMIN")) {
                // 如果是管理员，则查询所有操作日志
            } else {
                // 如果不是管理员，则查询当前用户的操作日志
                queryWrapper.eq("username", currentUser.getUsername());
            }
        }

        if (username != null && !username.isEmpty()) {
            queryWrapper.like("username", username);
        }

        if (operationType != null && !operationType.isEmpty()) {
            queryWrapper.eq("operation_type", operationType);
        }

        if (operationModule != null && !operationModule.isEmpty()) {
            queryWrapper.eq("operation_module", operationModule);
        }

        // 按操作时间降序排序
        queryWrapper.orderByDesc("operation_time");



        Page<OperationLog> page = operationLogService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    @GetMapping("getById/{id}")
    @Operation(summary = "根据ID获取操作日志")
    public Result<OperationLog> getOperationLogById(@PathVariable Integer id) {
        return Result.success(operationLogService.getById(id));
    }

    @DeleteMapping("delete/{id}")
    @Operation(summary = "删除操作日志")
    @Log(operationType = "删除", operationModule = "日志管理")
    public Result<Boolean> deleteOperationLog(@PathVariable Integer id) {
        return Result.success(operationLogService.removeById(id));
    }

    @DeleteMapping("batchDelete")
    @Operation(summary = "批量删除操作日志")
    @Log(operationType = "批量删除", operationModule = "日志管理")
    public Result<Boolean> batchDeleteOperationLog(@RequestBody List<Integer> ids) {
        return Result.success(operationLogService.removeByIds(ids));
    }
} 